<template>
  <div class="post-view" @click="lookPostClick">
    <div
      class="post-content"
      :class="{ 'post-content-no-img': !postItem.coverImg }"
    >
      <span class="title">{{ postItem.title }}</span>
      <span class="text">{{ postItem.summary }}</span>
      <div class="post-bottom">
        <div class="left">
          <span v-if="activeName !== 'first'">
            {{ postItem.username }}
          </span>
          <span v-if="activeName !== 'first'">|</span>
          <span>{{ timeAgo(postItem.createTime) }}</span>
          <span>|</span>
          <div class="icon">
            <svg-icon icon-file-name="eye"></svg-icon>
            <span style="margin-left: 5px">{{
              computedPageViewAndLikes(postItem.pageView)
            }}</span>
          </div>
          <div class="icon">
            <svg-icon icon-file-name="like"></svg-icon>
            <span style="margin-left: 5px">{{ postItem.likeCount }}</span>
          </div>
          <div class="icon">
            <svg-icon icon-file-name="comment"></svg-icon>
            <span style="margin-left: 5px">{{ postItem.commentCount }}</span>
          </div>
          <div
            class="icon"
            style="cursor: pointer"
            v-if="postItem.userId === userInfo.userId && activeName === 'first'"
          >
            <el-dropdown trigger="click">
              <span class="el-dropdown-link" @click.stop>
                <i class="el-icon-more"></i>
              </span>
              <!-- 选项是发表时渲染 -->
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item
                  icon="el-icon-edit"
                  @click.native="goToModifyPost"
                  >编辑</el-dropdown-item
                >
                <el-dropdown-item
                  icon="el-icon-delete"
                  @click.native="deletePost"
                  >删除</el-dropdown-item
                >
              </el-dropdown-menu>
            </el-dropdown>
          </div>
        </div>
        <div class="right">
          <el-tag
            size="mini"
            type="info"
            effect="plain"
            v-for="(name, index) in postItem.tagNames"
            :key="index"
            >{{ name }}</el-tag
          >
        </div>
      </div>
    </div>
    <div class="post-img" v-if="postItem.coverImg">
      <img :src="postItem.coverImg" alt="" draggable="false" />
    </div>
  </div>
</template>

<script>
import { mapState } from "vuex";
import { addPostPageView } from "../../../api/post";
export default {
  props: {
    postItem: {
      type: Object,
      default: () => ({}),
    },
    activeName: {
      type: String,
      default: "",
    },
  },
  data() {
    return {};
  },
  methods: {
    lookPostClick() {
      // 将当前访问的文章浏览量加一
      addPostPageView(this.postItem.postId).then((res) => {
        if (res.data.code === 1) {
          res;
        } else {
          this.$message.error("网络错误");
        }
      });
      // 使用Vue Router获取目标路由的完整URL
      const routePath = this.$router.resolve({
        name: "LookPost",
        params: {
          postId: this.postItem.postId,
          postTitle: this.postItem.title,
        },
      }).href;
      window.open(routePath, "_blank");
    },
    // 去修改文章
    goToModifyPost() {
      this.$router.push({
        name: "publication",
        params: {
          postId: this.postItem.postId,
          postData: this.postItem,
        },
      });
    },
    // 计算浏览量
    computedPageViewAndLikes(count) {
      if (count < 1000) {
        return count;
      } else {
        return (count / 1000).toFixed(1) + "k";
      }
    },
    // 计算日期函数
    timeAgo(pastDate) {
      const currentDate = new Date();
      const past = new Date(pastDate);
      const msPerDay = 24 * 60 * 60 * 1000;

      // 计算日期差异
      const diffInDays = Math.floor((currentDate - past) / msPerDay);
      const diffInMonths =
        currentDate.getMonth() -
        past.getMonth() +
        12 * (currentDate.getFullYear() - past.getFullYear());
      const diffInYears = currentDate.getFullYear() - past.getFullYear();

      // 根据日期差异返回不同的字符串
      if (diffInDays <= 30) {
        return `${diffInDays} 天前`;
      } else if (diffInMonths < 12) {
        return `${diffInMonths} 月前`;
      } else {
        return `${diffInYears} 年前`;
      }
    },
    // 删除文章
    deletePost() {
      this.$emit("deletePost", this.postItem.postId);
    },
  },
  computed: {
    ...mapState({
      userInfo: (state) => state.userInfo.userInfo,
    }),
  },
  mounted() {},
};
</script>

<style lang="less" scoped>
.post-view {
  cursor: pointer;
  width: 100%;
  display: flex;
  padding: 15px 5px 10px 5px;
  box-sizing: border-box;
  border-bottom: 1px solid #f5f5f5;
  .post-content {
    box-sizing: border-box;
    padding-right: 20px;
    padding-left: 10px;
    width: 80%;
    display: flex;
    flex-direction: column;
    justify-content: flex-start;
    gap: 10px;
    &.post-content-no-img {
      width: 100%;
    }
    .title {
      width: 100%;
      font-size: 17px !important;
      font-weight: bold !important;
    }
    .text {
      font-size: 14px !important;
      color: #8a919f !important;
      white-space: nowrap;
      overflow: hidden;
      text-overflow: ellipsis;
    }
    .post-bottom {
      margin-top: 7px;
      display: flex;
      justify-content: space-between;
      gap: 10px;
      font-size: 14px;
      color: #999;
      .left {
        display: flex;
        justify-content: flex-start;
        align-items: center;
        gap: 10px;
        .icon {
          display: flex;
          justify-content: center;
          align-items: center;
          .svg-icon {
            width: 15px;
            height: 15px;
          }
        }
      }
      .right {
        display: flex;
        justify-content: flex-end;
        align-items: center;
        gap: 10px;
      }
    }
  }
  .post-img {
    img {
      width: 130px;
      height: 80px;
      border-radius: 5px;
    }
  }
}
</style>